import grace from "../../utils/grace.js"
grace.page({
      data: {
            //wait_pay,wait_deliver,wait_receive,completed
            searchParam: {
                  status_tags: 'all',
                  pageSize: 10,
                  pageCurrent: 1
            },
            hasMore: true,
            result: []
      },
      onLoad: function(options) {
            let param = options[0][0];
            if (param && param['tag']) {
                  this.$data.searchParam.status_tags = param['tag'];
            }
            this.loadData();
      },
      switchTab: function(e) {
            var cur = e.target.dataset.current;
            if (this.$data.searchParam.status_tags != cur) {
                  this.$data.searchParam.status_tags = cur;
                  this.$data.searchParam.pageCurrent = 1;
                  this.loadData();
            }
      },
      loadData: function() {
            this.$http.get("api/order/user_order_list", this.data.searchParam).then((res) => {
                  wx.stopPullDownRefresh();
                  var isHasMore = res.list.length >= this.$data.searchParam.pageSize;
                  const data = this.$data.searchParam.pageCurrent == 1 ? res.list : this.$data.result.concat(res.list);
                  this.$data = {
                        result: data,
                        hasMore: isHasMore
                  };
            });
      },
      openDetail: function(e) {
            let index = e.currentTarget.dataset.index;
            index = parseInt(index);
            this.$data.result[index]['is_show'] = !this.$data.result[index]['is_show'];
      },

      repay: function(e) {
            let order_id = e.currentTarget.dataset.order;
            let order_amount = e.currentTarget.dataset.amount;
            wx.navigateTo({
                  url: '../pay/pay?order_id=' + order_id + '&order_amount=' + order_amount
            })
      },
      cancle: function (e) {
            let order_id = e.currentTarget.dataset.order;
            let that = this;
            wx.showModal({
                  title: '提示',
                  content: '确定要取消选中的订单吗？',
                  success: function (res) {
                        if (res.confirm) {
                              that.$http.post('api/order/cancel_order', {
                                    order_id: order_id
                              }).then((res) => {
                                    that.loadData();
                              });
                        }
                  }
            })
      },
      gocomment: function(e) {
            let order_id = e.currentTarget.dataset.order;
            wx.navigateTo({
                  url: '../order_comment/order_comment?order_id=' + order_id
            })
      },
      onReachBottom: function() {
            if (this.$data.hasMore) {
                  ++this.$data.searchParam.pageCurrent;
                  this.$data.hasMore = false;
                  this.loadData();
            }
      },
      $onBackData(data) {
            if (data && data.isUpdate) {
                  this.loadData();
            }
      }
})